The best practice is to open the file with the open function using a with block, which will ensure that the file gets closed for you at the end of the block.
Method 1: Print To File Using Write() We can directly write to the file using the built-in function write() that we learned in our file handling tutorial.
Write to an Existing File. To write to an existing file, you must add a parameter to the open() function: a - Append - will append to the end of the file.
To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file.